/*
Copyright (C) 2008 Matthew Perry

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This libary/program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using BaseTools.Configuration;

namespace BaseTools.Helpers
{
	using GenericSection = Dictionary<string, string>; 
	using GenericApplication = Dictionary<string, Dictionary<string, string>>;
	
	/// <summary>
	/// Converts between collections
	/// </summary>
	public class GenericConverter : SharedTag
	{
		/// <summary>
		/// Read a single-scope ConfigList into a dictionary
		/// </summary>
		/// <param name="section">The ConfigList to convert</param>
		/// <returns>Dictionary copy of section</returns>
		public static GenericSection ReadSection(ConfigList section)
		{
			GenericSection gs = new GenericSection();
			
			List<ConfigPair> cpair = section.GetList();
			
			foreach(ConfigPair p in cpair)
			{
				if (p.Data != null && !gs.ContainsKey(p.Key))
					gs.Add(p.Key, p.Data.ToString());
			}
			
			return gs;
		}
		
		/// <summary>
		/// Read a double-scope ConfigList into a dictionary
		/// </summary>
		/// <param name="application">The ConfigList to convert</param>
		/// <returns>Dictionary copy of application</returns>
		public static GenericApplication ReadApplication(ConfigList application)
		{
			GenericApplication ga = new GenericApplication();
			
			List<ConfigPair> cpair = application.GetList();
			
			foreach(ConfigPair p in cpair)
			{
				if (p.DataIsList && !ga.ContainsKey(p.Key))
					ga.Add(p.Key, ReadSection(p.DataAsList));
			}
			
			return ga;
		}
		
		/// <summary>
		/// Write a single-scope dictionary into a ConfigList
		/// </summary>
		/// <param name="section">The dictionary to convert</param>
		/// <returns>ConfigList copy of section</returns>
		public static ConfigList WriteSection(GenericSection section)
		{
			ConfigList cfg = new ConfigList();
			
			foreach(KeyValuePair<string, string> e in section)
			{
				cfg.AddString(e.Key, e.Value);
			}
			
			return cfg;
		}
		
		/// <summary>
		/// Write a double-scope dictionary into a ConfigList
		/// </summary>
		/// <param name="application">The dictionary to convert</param>
		/// <returns>ConfigList copy of application</returns>
		public static ConfigList WriteApplication(GenericApplication application)
		{
			ConfigList cfg = new ConfigList();
			
			foreach(KeyValuePair<string, GenericSection> e in application)
			{
				cfg.AddSection(e.Key, WriteSection(e.Value));
			}
			
			return cfg;
		}
	}
}
